package dai.core.base.camera

import dai.core.base.MyLog
import dai.core.base.Utils
import dai.core.base.Window
import dai.core.base.camera.Camera
import org.joml.Vector3f
import org.lwjgl.glfw.GLFW
import kotlin.math.cos
import kotlin.math.sin

class QuakeCamera(position:Vector3f) : Camera(position) {

    //  角度
    var lon = 0f
    var phi = 0f

    var forward = false
    var backward = false
    var leftward = false
    var rightward = false

    var mouseX = 0f
    var mouseY = 0f

    var mouseDown = false

    fun input(window: Window){
        when {
            // escape
            window.isKeyPress(GLFW.GLFW_KEY_ESCAPE) -> GLFW.glfwSetWindowShouldClose(window.winHandler, true)

            // 前进
            window.isKeyPress(GLFW.GLFW_KEY_UP) ||
                    window.isKeyPress(GLFW.GLFW_KEY_W) -> {
                forward = true
                MyLog.info("### forward is $forward")
            }

            window.isKeyUp(GLFW.GLFW_KEY_UP,forward) ||
                    window.isKeyUp(GLFW.GLFW_KEY_W,forward) -> {
                forward = false
                MyLog.info("### forward is $forward")
            }

            // 后退
            window.isKeyPress(GLFW.GLFW_KEY_DOWN) ||
                    window.isKeyPress(GLFW.GLFW_KEY_S) -> {
                backward = true
                MyLog.info("### backward is $backward")
            }
            window.isKeyUp(GLFW.GLFW_KEY_DOWN,backward) ||
                    window.isKeyUp(GLFW.GLFW_KEY_S,backward) -> {
                backward = false
                MyLog.info("### backward is $backward")
            }


            // 左
            window.isKeyPress(GLFW.GLFW_KEY_LEFT) ||
                    window.isKeyPress(GLFW.GLFW_KEY_A) -> {
                leftward = true
                MyLog.info("### leftward is $leftward")
            }
            window.isKeyUp(GLFW.GLFW_KEY_LEFT,leftward) ||
                    window.isKeyUp(GLFW.GLFW_KEY_A,leftward) -> {
                leftward = false
                MyLog.info("### leftward is $leftward")
            }

            // 右
            window.isKeyPress(GLFW.GLFW_KEY_RIGHT) ||
                    window.isKeyPress(GLFW.GLFW_KEY_D) -> {
                rightward = true
                MyLog.info("### rightward is $rightward")
            }

            window.isKeyUp(GLFW.GLFW_KEY_RIGHT,rightward) ||
                    window.isKeyUp(GLFW.GLFW_KEY_D,rightward) -> {
                rightward = false
                MyLog.info("### rightward is $rightward")
            }
        }
    }

    fun update(dt:Float){
        when{
            forward -> translateZ(-0.1f,true)
            backward -> translateZ(0.1f,true)
            leftward -> translateX(0.1f,true)
            rightward -> translateX(-0.1f,true)
        }

        if (mouseDown){
            // 更新 target
            lon = Utils.mapFloat(mouseX,-400f,400f,45f,90f)
            phi = Utils.mapFloat(mouseY,-300f,300f,-40f,40f)
            MyLog.info("mouseX:${mouseX.toInt()},mouseY:${mouseY.toInt()}")
            MyLog.info("lon:${lon.toInt()},phi:${phi.toInt()}")
            val rad_lon = Utils.toRadius(lon)
            val rad_phi = Utils.toRadius(90f - phi)

            val len = 5f
            target.x = position.x - len * sin(rad_phi) * cos(rad_lon)
            target.y = position.y - len * cos(rad_phi)
            target.z = position.z - len * sin(rad_phi) * sin(rad_lon)
        }
    }
}